package com.example.lenovo.downloaddemo.activity;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Request;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.lenovo.downloaddemo.R;
import com.example.lenovo.downloaddemo.bean.Customer;
import com.example.lenovo.downloaddemo.bean.ReturnBean;
import com.example.lenovo.downloaddemo.http.datasource.MDataSource;
import com.example.lenovo.downloaddemo.http.okhttp.utils.AppContext;
import com.example.lenovo.downloaddemo.http.okhttp.utils.CodeUtils;
import com.example.lenovo.downloaddemo.http.okhttp.utils.Collintion;
import com.example.lenovo.downloaddemo.http.okhttp.utils.CommonUtils;
import com.example.lenovo.downloaddemo.http.okhttp.utils.MD5;
import com.example.lenovo.downloaddemo.http.okhttp.utils.ToastUtil;
import com.example.lenovo.downloaddemo.myview.MyEditText;
import com.example.lenovo.downloaddemo.sqlite.LoadListCallBack;
import com.example.lenovo.downloaddemo.util.DeviceUuidFactory;
import com.example.lenovo.downloaddemo.util.PhoneUtil;
import com.example.lenovo.downloaddemo.util.StatusBarUtil;
import com.example.lenovo.downloaddemo.util.TimeUtil;
import com.example.lenovo.downloaddemo.view.TitleBarView;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.List;

public class RegisterActivity extends BaseActivity implements View.OnClickListener {
    @Bind(R.id.register_titlebar)
    TitleBarView titleBarView;
    @Bind(R.id.register_phone_editText)
    MyEditText register_phone_editText;//手机号
    @Bind(R.id.register_password_editText)
    MyEditText register_password_editText;//密码
    @Bind(R.id.register_password_again_editText)
    MyEditText register_password_again_editText;//确认密码
    @Bind(R.id.image_code)
    ImageView image_code;//图文验证码
    @Bind(R.id.input_message_code)
    MyEditText input_message_code;//验证码

    @Bind(R.id.regist_xieyi)
    TextView regist_xieyi;

    @Bind(R.id.regist_comfirm_button)
    TextView regist_comfirm_button;//注册

    @Bind(R.id.go_login)
    TextView go_login;//已有账号 去登陆

    private String imageCode;//图文验证码

    private String allString;

    private Handler mHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==1){
                String returnMessage= (String) msg.obj;
                Gson gson=new Gson();
                ReturnBean bean=gson.fromJson(returnMessage,ReturnBean.class);
                if(bean.getCode()==200){
                    ToastUtil.makeText(AppContext.getInstance(),"注册成功");
                    Intent intent=new Intent(RegisterActivity.this,LoginActivity.class);


                    startActivity(intent);
                    finish();

                }else {
                    String error=bean.getMsg().toString();
                    ToastUtil.makeText(AppContext.getInstance(),error);
                }

            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
        initView();
    }

    @Override
    protected void initView() {
        StatusBarUtil.setMargin(AppContext.getInstance(),titleBarView);
        titleBarView.showBackButton(true);
        titleBarView.setTitle(AppContext.getInstance().getResources().getString(R.string.zhuce_cn));
        image_code.setImageBitmap(CodeUtils.getInstance().createBitmap());
        imageCode=CodeUtils.getInstance().getCode().toLowerCase();

        register_phone_editText.setHint(AppContext.getInstance().getResources().getText(R.string.tel_ch));
        register_password_editText.setHint(AppContext.getInstance().getResources().getText(R.string.regist_inputPhone_cn));
        register_password_again_editText.setHint(AppContext.getInstance().getResources().getText(R.string.regist_inputPhone_again_cn));
        input_message_code.setHint(AppContext.getInstance().getResources().getText(R.string.regist_inputMessageCode_cn));
        regist_comfirm_button.setText(AppContext.getInstance().getResources().getText(R.string.zhuce_cn));

        go_login.setText(AppContext.getInstance().getResources().getText(R.string.go_login_cn));
        regist_xieyi.setText(AppContext.getInstance().getResources().getText(R.string.zhuce_xieyi));
        Log.e("图文验证码===",imageCode);
        initListener();

    }

    private void saveUserInfo(){
        String phone=register_phone_editText.getText().toString().trim();//手机号
        String password=register_password_editText.getText().toString().trim();//密码
        String password_again=register_password_again_editText.getText().toString().trim();//确认密码
        String code=input_message_code.getText().toString().trim();//验证码
        DeviceUuidFactory deviceUuidFactory=new DeviceUuidFactory(AppContext.getInstance());
        String setting_id=deviceUuidFactory.getDeviceUuid().toString();
        Log.e("UUID===",setting_id);


        if(!TextUtils.isEmpty(phone)&&!TextUtils.isEmpty(password)&&!TextUtils.isEmpty(password_again)&&!TextUtils.isEmpty(code)&&!TextUtils.isEmpty(setting_id)){
            if(PhoneUtil.isTel(phone)) {
                if(password.equals(password_again)){
                    if (code.equals(imageCode)) {
                        regist_Data(phone, password, code, setting_id);
                    }else {
                        ToastUtil.makeText(AppContext.getInstance(),"验证码不正确");
                        image_code.setImageBitmap(CodeUtils.getInstance().createBitmap());
                        imageCode=CodeUtils.getInstance().getCode().toLowerCase();
                    }
                }else{
                    ToastUtil.makeText(AppContext.getInstance(),"两次输入的密码不相同");
                    image_code.setImageBitmap(CodeUtils.getInstance().createBitmap());
                    imageCode=CodeUtils.getInstance().getCode().toLowerCase();
                }
            }else{
                ToastUtil.makeText(AppContext.getInstance(),"手机格式不正确");
                image_code.setImageBitmap(CodeUtils.getInstance().createBitmap());
                imageCode=CodeUtils.getInstance().getCode().toLowerCase();
            }

        }else{
            ToastUtil.makeText(AppContext.getInstance(),"不能为空");
            image_code.setImageBitmap(CodeUtils.getInstance().createBitmap());
            imageCode=CodeUtils.getInstance().getCode().toLowerCase();
        }



    }

    private void regist_Data(String phone,String password,String code,String setting_id){
        HashMap<String, String> map = new HashMap<>();
        map.put("model", "Login");
        map.put("method", "register");
        map.put("user_phone", phone);
        map.put("password", password);
        map.put("code", code);
        map.put("setting_id", setting_id);
        map.put("time",new TimeUtil().getMoment());
        Collintion collintion=new Collintion();

        allString=collintion.Sort(map)+"ad1af567dc7bfedd5e760fc1f48a59ae";
        String sign= MD5.GetMD5Code(MD5.GetMD5Code(allString));
        Log.e("拼接字符串",allString);
        Log.e("签名===",sign);
        map.put("sign",sign);

        MDataSource<Customer> dataSource = new MDataSource<>(AppContext.getInstance());
        dataSource.postData(map, new LoadListCallBack() {
            @Override
            public void loadList(List list) {

            }

            @Override
            public void systemError(Request request, String errorInfo, Exception e) {
                Log.e("error", "systemError: " + errorInfo);
                e.printStackTrace();
            }

            @Override
            public void retLoad(String code) {

            }

            @Override
            public void loadString(String string) {
                Log.e("注册", "loadString 获取结果 : " + string);

                Message message=Message.obtain();
                message.what=1;
                message.obj=string;
                mHandler.sendMessage(message);





            }
        });
    }

    @Override
    protected void initListener() {
        image_code.setOnClickListener(this);
        regist_comfirm_button.setOnClickListener(this);
        go_login.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.image_code:
                image_code.setImageBitmap(CodeUtils.getInstance().createBitmap());
                imageCode=CodeUtils.getInstance().getCode().toLowerCase();
                Log.e("刷新验证码====",imageCode);
                break;
            case R.id.regist_comfirm_button:

                if(CommonUtils.isNetOk(AppContext.getInstance())) {
                    saveUserInfo();
                }else{
                    ToastUtil.makeText(AppContext.getInstance(),"Fail!");
                }



                break;
            case R.id.go_login:
//                Intent intent=new Intent(RegisterActivity.this,LoginActivity.class);
//                startActivity(intent);
                finish();
                break;

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        finish();
    }
}
